package com.saifan.wyy_ov.ui.communitylife;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.BBSThemesBean;
import com.saifan.wyy_ov.data.bean.RequestBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: Wangli
 * @date: 2015-6-9 15:18:40
 * @descrption 业主论坛
 */

public class OwnerBBSActivity extends BaseActivity {
    private android.support.v7.widget.Toolbar toolbar;
    private android.widget.ListView listView;
    private IGetData getData;
    private List<BBSThemesBean> list;
    private CommonAdapter<BBSThemesBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupViews();
        initData();
    }

    private void setupViews() {
        setContentView(R.layout.activity_owner_bbs);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        list =  new ArrayList<BBSThemesBean>();
        adapter = new CommonAdapter<BBSThemesBean>(OwnerBBSActivity.this, list, R.layout.owner_bbs_item) {
            @Override
            public void convert(ViewHolderHelper helper, BBSThemesBean item) {
                helper.setText(R.id.title, StringUtil.returnShow(item.getLTBK_MC()));
                helper.setImageByUrl(R.id.header, item.getWYKHDA_TXLJ());
                if (StringUtil.isNullOrEmpty(item.getFTB_BT())){
                    helper.setText(R.id.content, "业主论坛最新模块，欢迎加入话题讨论");
                } else {
                    helper.setText(R.id.content, item.getFTB_BT());
                }
                int id = getResources().getIdentifier("bbs_0" + (helper.getPosition() + 1), "drawable", getPackageName());
                helper.setImageResource(R.id.bbs_ico, id);
            }
        };
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(OwnerBBSActivity.this, BBSListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", list.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initData(){
        getData = new GetData();
        RequestBean requestBean = new RequestBean();
        requestBean.setXMBS(getRoomInfoBean().getXMBS());
        getData.getDataJson(OwnerBBSActivity.this, Api.BBS.BBSTHEMES, requestBean, "正在加载...", new ResponseData() {
            @Override
            public void onSuccess(String json) {
                List<BBSThemesBean> tempList = (new Gson()).fromJson(json, new TypeToken<List<BBSThemesBean>>() {
                }.getType());
                if (tempList != null && tempList.size() > 0) {
                    Message message = handler.obtainMessage();
                    message.obj = tempList;
                    message.what = 1;
                    handler.sendMessage(message);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }

            @Override
            public void onFailed(String str) {

            }
        });
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                list = (List<BBSThemesBean>) msg.obj;
                adapter.mDatas = list;
                adapter.notifyDataSetChanged();
            } else if (msg.what == 0){
                ToastUtil.showToast(OwnerBBSActivity.this, "暂无数据");
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_owner_bbs, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.my_publish){
            startActivity(new Intent(OwnerBBSActivity.this, MyPublishActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
